/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.notify.web;

import com.thinkgem.jeesite.androidpn.server.xmpp.presence.PresenceManager;
import com.thinkgem.jeesite.androidpn.server.xmpp.session.SessionManager;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.notify.entity.NotiUser;
import com.thinkgem.jeesite.modules.notify.service.NotiUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 推送用户Controller
 * @author chenbin
 * @version 2018-12-14
 */
@Controller
@RequestMapping(value = "${adminPath}/notify/notiUser")
public class NotiUserController extends BaseController {

	@Autowired
	private NotiUserService notiUserService;
	
	@ModelAttribute
	public NotiUser get(@RequestParam(required=false) String id) {
		NotiUser entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = notiUserService.get(id);
		}
		if (entity == null){
			entity = new NotiUser();
		}
		return entity;
	}
	
	@RequiresPermissions("notify:notiUser:view")
	@RequestMapping(value = {"list", ""})
	public String list(NotiUser notiUser, HttpServletRequest request, HttpServletResponse response, Model model) {
		Integer onlinePersons = SessionManager.getInstance().getClientSessions().size();
		model.addAttribute("onlinePersons", onlinePersons);
		Page<NotiUser> page = notiUserService.findPage(new Page<NotiUser>(request, response), notiUser);
		PresenceManager presenceManager = new PresenceManager();
		presenceManager.isAvailableUsers(page.getList());
		model.addAttribute("page", page);
		return "modules/notify/notiUserList";
	}

	@RequiresPermissions("notify:notiUser:view")
	@RequestMapping(value = "form")
	public String form(NotiUser notiUser, Model model) {
		model.addAttribute("notiUser", notiUser);
		return "modules/notify/notiUserForm";
	}

	@RequiresPermissions("notify:notiUser:edit")
	@RequestMapping(value = "save")
	public String save(NotiUser notiUser, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, notiUser)){
			return form(notiUser, model);
		}
		notiUserService.save(notiUser);
		addMessage(redirectAttributes, "保存推送用户成功");
		return "redirect:"+Global.getAdminPath()+"/notify/notiUser/?repage";
	}
	
	@RequiresPermissions("notify:notiUser:edit")
	@RequestMapping(value = "delete")
	public String delete(NotiUser notiUser, RedirectAttributes redirectAttributes) {
		notiUserService.delete(notiUser);
		addMessage(redirectAttributes, "删除推送用户成功");
		return "redirect:"+Global.getAdminPath()+"/notify/notiUser/?repage";
	}

}